using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;

namespace Universe.WinForms
{
    [Serializable]
    public class RoundRectPath
    {
        public enum Shape
        {
            RoundRect,
            CutRect
        }
        
        Rectangle _Rectangle;
        Size _Radius;
        Shape _TheShape;

        public RoundRectPath(Rectangle rectangle, int radius)
            : this(rectangle, new Size(radius, radius))
        {
        }

        public RoundRectPath(Rectangle rectangle, Size radius)
            : this(rectangle, radius, Shape.RoundRect)
        {
        }

        public RoundRectPath(Rectangle rectangle, Size radius, Shape theShape)
        {
            _Rectangle = rectangle;
            _Radius = radius;
            _TheShape = theShape;
        }

        public Shape TheShape
        {
            get { return _TheShape; }
            set { _TheShape = value; }
        }
        
        public Rectangle Rectangle
        {
            get { return _Rectangle; }
            set { _Rectangle = value; }
        }

        public Size Radius
        {
            get { return _Radius; }
            set { _Radius = value; }
        }

        public GraphicsPath GraphicsPath
        {
            get
            {
                GraphicsPath ret =
                    TheShape == Shape.RoundRect
                        ? generateRoundRect(Rectangle, Radius.Width, Radius.Height, false, false, false, false)
                        : generateCutRect(Rectangle, Radius.Width, Radius.Height);
                
                ret.CloseFigure();
                return ret;
            }
        }
        
        private GraphicsPath generateCutRect(Rectangle rect, int cutX, int cutY)
        {
            GraphicsPath path = new GraphicsPath();

            path.AddLine(rect.Left + cutX, rect.Top, rect.Right - cutX, rect.Top);
            path.AddLine(rect.Right - cutX, rect.Top, rect.Right, rect.Top + cutY);
            
            path.AddLine(rect.Right, rect.Top + cutY, rect.Right, rect.Bottom - cutY);
            path.AddLine(rect.Right, rect.Bottom - cutY, rect.Right - cutX, rect.Bottom);
            
            path.AddLine(rect.Right - cutX, rect.Bottom, rect.Left + cutX, rect.Bottom);
            path.AddLine(rect.Left + cutX, rect.Bottom, rect.Left, rect.Bottom - cutY);
            
            path.AddLine(rect.Left, rect.Bottom - cutY, rect.Left, rect.Top + cutY);
            path.AddLine(rect.Left, rect.Top + cutY, rect.Left + cutX, rect.Top);
            
            return path;
        }

        private GraphicsPath generateRoundRect(Rectangle rect, int radiusX, int radiusY, bool leftTop, bool rightTop, bool rightBottom, bool leftBottom)
        {
            GraphicsPath path = new GraphicsPath();
            // top
            path.AddLine(rect.Left + radiusX, rect.Top, rect.Right - radiusX, rect.Top);
            path.AddArc(rect.Right - radiusX * 2, rect.Top, radiusX * 2, radiusY * 2, -90, 90);
            
            path.AddLine(rect.Right, rect.Top + radiusY, rect.Right, rect.Bottom - radiusY);
            path.AddArc(rect.Right - radiusX * 2, rect.Bottom - radiusY * 2, radiusX * 2, radiusY * 2, 0, 90);
            
            path.AddLine(rect.Right - radiusX, rect.Bottom, rect.Left + radiusX, rect.Bottom);
            path.AddArc(rect.Left, rect.Bottom - radiusY * 2, radiusX * 2, radiusY * 2, 90, 90);
            
            path.AddLine(rect.Left, rect.Bottom - radiusY, rect.Left, rect.Top + radiusY);
            path.AddArc(rect.Left, rect.Top, radiusX * 2, radiusY * 2, 180, 90);


/*
            path.AddLine(rect.Left + (leftTop ? radius : 0), rect.Top, rect.Right - (rightTop ? radius : 0), rect.Top);

            if (rightTop == true)
                path.AddArc(rect.Right - radius * 2, rect.Top, radius * 2, radius * 2, -90, 90);

            path.AddLine(rect.Right, rect.Top + (rightTop ? radius : 0), rect.Right, rect.Bottom - (rightBottom ? radius : 0));

            if (rightBottom == true)
                path.AddArc(rect.Right - radius * 2, rect.Bottom - radius * 2, radius * 2, radius * 2, 0, 90);

            path.AddLine(rect.Right - (rightBottom ? radius : 0), rect.Bottom, rect.Left + (leftBottom ? radius : 0), rect.Bottom);

            if (leftBottom == true)
                path.AddArc(rect.Left, rect.Bottom - radius * 2, radius * 2, radius * 2, 90, 90);

            path.AddLine(rect.Left, rect.Bottom - (leftBottom ? radius : 0), rect.Left, rect.Top + (leftTop ? radius : 0));

            if (leftTop == true)
                path.AddArc(rect.Left, rect.Top, radius * 2, radius * 2, 180, 90);


*/
            return path;
        }

 


        
    }
}
